/**
 * 神州信息(2012)
 */
package net.abbp.report.view.cell.painter;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.util.Iterator;

import net.abbp.report.model.Cell;
import net.abbp.report.model.ReportModel;

import com.reportgear.report.model.auxiliary.ColumnPropList;
import com.reportgear.report.model.auxiliary.RowPropList;
import com.reportgear.report.view.grid.util.GraphHelper;
import com.reportgear.report.view.grid.util.PaintUtils;

/**
 * 单元格中放置报表,或者报表模型
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 * 
 *        <pre>
 * 历史：
 *      建立: 2012-7-21 lexloo
 * </pre>
 */
public class ReportCellPainter implements ICellPainter {

    @Override
    public void paint(Graphics g, Rectangle rect, Cell cell) {
        ReportModel model = (ReportModel) cell.getValue();

        Graphics2D g2 = (Graphics2D) g;

        ColumnPropList cpl = model.getColumnPropList();
        RowPropList crl = model.getRowPropList();

        Shape oldClip = g2.getClip();
        g2.clip(rect);
        g2.translate(rect.x, rect.y);
        // 绘制线条
        this.drawGridLine(g2, model, cpl, crl);

        // 绘制对象
        this.paintCell(g2, model, cpl, crl);

        g2.translate(-rect.x, -rect.y);
        g2.setClip(oldClip);
    }

    private void paintCell(Graphics2D g2, ReportModel model, ColumnPropList cpl, RowPropList crl) {
        for (Iterator<Cell> itr = model.getCellIterator(); itr.hasNext();) {
            Cell cell = itr.next();
            Rectangle r = getCellRectangle(cell, cpl, crl);

            PaintUtils.paintCellElement(g2, cell, r);
        }
    }

    private Rectangle getCellRectangle(Cell cell, ColumnPropList cpl, RowPropList crl) {
        int column = cell.getColumn();
        int row = cell.getRow();
        int columnSpan = cell.getColumnSpan();
        int rowSpan = cell.getRowSpan();

        int startX = cpl.getRangeValue(0, column);
        int startY = crl.getRangeValue(0, row);

        int width = 0;
        for (int i = 0; i < columnSpan; i++) {
            width += cpl.getWidth(column + i);
        }

        int height = 0;
        for (int j = 0; j < rowSpan; j++) {
            height += crl.getHeight(row + j);
        }

        return new Rectangle(startX, startY, width, height);
    }

    // 绘制网格线
    private void drawGridLine(Graphics2D g2, ReportModel model, ColumnPropList cpl, RowPropList crl) {
        // 绘制行线
        g2.setColor(Color.BLACK);

        int rowCount = model.getRowCount();
        int columnCount = model.getColumnCount();

        int startX = 0;
        int endX = 0;
        for (int i = 0; i < columnCount; i++) {
            endX += cpl.getWidth(i);
        }

        int startY = 0;
        int endY = 0;
        for (int i = 0; i < rowCount; i++) {
            endY += crl.getHeight(i);
        }

        int next = startY;
        for (int i = 0; i < rowCount; i++) {
            GraphHelper.drawLine(g2, startX, next, endX, next);
            next += crl.getHeight(i);
        }
        GraphHelper.drawLine(g2, startX, next, endX, next);

        next = startX;
        for (int i = 0; i < columnCount; i++) {
            GraphHelper.drawLine(g2, next, startY, next, endY);
            next += cpl.getWidth(i);
        }
        GraphHelper.drawLine(g2, next, startY, next, endY);
    }
}
